import 'package:bxultimate/app/router/router_name.dart';
import 'package:bxultimate/data/request/application_request.dart';
import 'package:bxultimate/data/request/base_request.dart';
import 'package:bxultimate/domain/common/MapLatLonDto.dart';
import 'package:bxultimate/domain/common/result.dart';
import 'package:bxultimate/shared/shared.dart';
import 'package:get/get.dart';
import 'package:amap_flutter_base/amap_flutter_base.dart';
import 'state.dart';

class ExaminationReadyController extends GetxController {
  final ExaminationReadyState state = ExaminationReadyState();
  late LatLng userPosition;

  final examId = Get.arguments['id'];
  final time = Get.arguments['time'];

  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();

    // 获取当前位置
    getPosition();
  }

  getPosition() async {

    // 检测是否超时
    Result result = await ApplicationRequest.checkExamIsTimeout(examId);
    if(result.data['isTimeout']){
      Get.snackbar('提示', '考试已结束');
      Get.back();
    }
    // 是否交卷
     result = await ApplicationRequest.panduanjiaojuan(examId);
    if(result.data['flag'] =='ywc'){
      Get.snackbar('提示', '考试已交卷');
      Get.back();
    }



    String? positon =
        UtilSp.getString(AppConstant.DEVICE_POSITION, defValue: '');
    var arr = positon!.split(',');
    userPosition = new LatLng(double.parse(arr[1]), double.parse(arr[0]));

    state.mapDto = await BaseRequest.getLatLonAddress(
        userPosition.latitude, userPosition.longitude);
  }

  void startExamination() {
    // 进入考试



    Get.toNamed(RouterName.examinationAnswerCard,arguments: {
      'examId': examId,
      'time': time,
      "lat": userPosition.latitude,
      "lon": userPosition.longitude,
      "countyCode": state.mapDto.countyCode
    });
  }
}
